package com.simple.datax.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.datax.bo.IDataxUserInfoBO;
import com.simple.datax.dao.IDataxUserInfoDAO;
import com.simple.datax.vo.DataxUserInfoVO;
import com.simple.util.common.Constants;
import com.simple.util.common.EncryptUtil;
import com.simple.util.exception.SimpleApplicationException;
import com.simple.util.object.datax.DataxUserBean;

@Service("dataxUserInfoBO")
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class DataxUserInfoBOImpl implements IDataxUserInfoBO {

	@Autowired
	private IDataxUserInfoDAO dataxUserInfoDAO;
	
	@Override
	public DataxUserInfoVO findById(final String userId) {
		return dataxUserInfoDAO.findById(userId);
	}

	@Override
	public List<DataxUserInfoVO> findAll() {
		return dataxUserInfoDAO.findAll();
	}

	@Override
	public DataxUserInfoVO doLogin(String loginId, String password) throws SimpleApplicationException {
		final DataxUserInfoVO dataxUserInfoVO = dataxUserInfoDAO.findByLoginId(loginId);
		if(dataxUserInfoVO == null){
            throw new SimpleApplicationException("message.login.loginid.not.exist");
        }
		
		if(Constants.STATE_STOP.equals(dataxUserInfoVO.getStatus())){
            throw new SimpleApplicationException("message.login.loginid.not.exist");
        }
		
        if (!EncryptUtil.passwordCheck(password, dataxUserInfoVO.getPassword())) {
            throw new SimpleApplicationException("message.login.loginid.password.error");
        } 
        
        return dataxUserInfoVO;
	}
	
	@Override
	public DataxUserBean getUserBean(final String userId){
		final DataxUserInfoVO dataxUserInfoVO = dataxUserInfoDAO.findById(userId);
		final DataxUserBean userBean = new DataxUserBean();
		userBean.setDataxUserInfoVO(dataxUserInfoVO);
		return userBean;
	}
	

}
